﻿using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEditor.IMGUI.Controls;
using UnityEngine;

namespace Simulation.Framework.Editor
{
    public class PanelTreeViewWindow : EditorWindow
    {
        [SerializeField] private TreeViewState scriptsTreeViewState;
        [SerializeField] private TreeViewState panelTreeViewState;

        private PanelPrefabTreeView prefabTreeView;
        private PanelScriptsTreeView scriptsTreeView;

        private Rect prefabTreeViewRect{ get { return new Rect(220, 10, position.width - 230, position.height - 40); }}
        private Rect scriptsTreeViewRect { get { return new Rect(10, 10, 200, position.height - 20); } }
        private Rect bottomAreaRect { get { return new Rect(220, position.height - 25, position.width - 230, 20); } }

        public Type selectedScript { get; private set; }

        [MenuItem("SimulationFramework/TreeViewWindow")]
        public static void CreatePanelWindow()
        {
            WindowHelper.CreateTreeViewWindow();
        }

        private void OnEnable()
        {
            OpenPrefabTreeView();
            OpenScriptsTreeView();
        }

        private void OnSelectionChange()
        {
            if (prefabTreeView != null)
                prefabTreeView.SetSelection(Selection.instanceIDs);
            if (scriptsTreeView != null)
                scriptsTreeView.SetSelection(Selection.instanceIDs);
            Repaint();
        }

        private void OnGUI()
        {
            DoPrefabTreeView();
            DoScriptTreeView();
            DoBottom();
        }

        #region GUI

        private void DoPrefabTreeView()
        {
            prefabTreeView.OnGUI(prefabTreeViewRect);
        }

        private void DoScriptTreeView()
        {
            scriptsTreeView.OnGUI(scriptsTreeViewRect);
            DoBottom();
        }

        private void DoBottom()
        {
            GUILayout.BeginArea(bottomAreaRect);
            GUILayout.BeginHorizontal();

            if (GUILayout.Button("取消"))
            {
                //Close();
                prefabTreeView.Reload();
            }

            if (GUILayout.Button("确定"))
            {
                prefabTreeView.WriteScript();
            }

            GUILayout.EndHorizontal();
            GUILayout.EndArea();
        }

        #endregion

        private void OpenPrefabTreeView()
        {
            if (panelTreeViewState == null)
                panelTreeViewState = new TreeViewState();
            float treeViewWidth = prefabTreeViewRect.width;
            MultiColumnHeaderState multiColumnHeaderState = PanelPrefabTreeView.CreateDefaultMultiColumnHeaderState(treeViewWidth);
            MultiColumnHeader multiColumnHeader = new MultiColumnHeader(multiColumnHeaderState);
            prefabTreeView = new PanelPrefabTreeView(panelTreeViewState, multiColumnHeader);
        }

        private void OpenScriptsTreeView()
        {
            if (scriptsTreeViewState == null)
                scriptsTreeViewState = new TreeViewState();
            float treeViewWidth = scriptsTreeViewRect.width;
            MultiColumnHeaderState multiColumnHeaderState = PanelScriptsTreeView.CreateDefaultMultiColumnHeaderState(treeViewWidth);
            MultiColumnHeader multiColumnHeader = new MultiColumnHeader(multiColumnHeaderState);
            scriptsTreeView = new PanelScriptsTreeView(scriptsTreeViewState, multiColumnHeader);
            scriptsTreeView.onItemSingleClciked = ChangePreafab;
        }

        private void ChangePreafab(PanelCodeData panelCodeData)
        {
            List<Transform> roots = FindPrefab(panelCodeData.mainScript.GetClass());
            prefabTreeView.SetRoot(roots, panelCodeData);
        }

        private List<Transform> FindPrefab(Type panelType)
        {
            List<Transform> traList = new List<Transform>();
            string[] guids = AssetDatabase.FindAssets("t:GameObject");
            for (int i = 0; i < guids.Length; i++)
            {
                string guid = guids[i];
                string path = AssetDatabase.GUIDToAssetPath(guid);
                GameObject go = AssetDatabase.LoadAssetAtPath<GameObject>(path);
                PanelBase panel = go.GetComponent(panelType) as PanelBase;
                if (panel != null)
                    traList.Add(go.transform);
            }
            return traList;
        }

    }
}